package sn.gandal.agriculture.agrimobile.j2me.httpServices;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.StatutConnexion;

/**
 *
 * @author dysow
 */
public class NetworkUtility
{

    public NetworkUtility()
    {
    }

    public byte[] getDataBytes(String url, Vector vecParameters) throws Exception
    {
        
	HttpConnection c = null;
        
	InputStream is = null;
	DataOutputStream dos = null;
	NetworkUrlCreator creator = new NetworkUrlCreator();
	int rc;
        String parameterString ="" ;//creator.getParametersString(vecParameters);
        System.out.println("NetworkUtility.getBytes ("+url +","+ "[ "+parameterString+ " ]"+" )");
	c = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
	c.setRequestMethod(HttpConnection.POST);
	c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//	c.setRequestProperty("Accept-Encoding", "gzip, deflate");	
	c.setRequestProperty("Accept-Charset","utf-8");
	c.setRequestProperty("Accept-Encoding","utf-8");
        c.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
	dos = c.openDataOutputStream();
	byte[] requestBody = parameterString.getBytes();

	for (int i = 0;i < requestBody.length;i++)
	{
	    dos.writeByte(requestBody[i]);
	}

//	dos.flush();
	dos.close();

	rc = c.getResponseCode();

	if (rc != HttpConnection.HTTP_OK)
	{
	    c.close();
	    throw new IOException("HTTP response code: " + rc);
	}

	is = c.openInputStream();

	StringBuffer str = new StringBuffer();
	int ch;

	while ((ch = is.read()) != -1)
	{
	    str.append((char)ch);
	}

	c.close();

	String ret = new String(str);

//	System.out.println(ret);//debug

	return ret.getBytes();
    }

    public byte[] sendDataBytes(String url, Vector vecParameters, String data) throws Exception
    {
	HttpConnection c = null;
	InputStream is = null;
	DataOutputStream dos = null;
	NetworkUrlCreator creator = new NetworkUrlCreator();
	int rc;

	c = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
	c.setRequestMethod(HttpConnection.POST);

	String parameterString = ""; // creator.getParametersString(vecParameters);

	c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//	c.setRequestProperty("Accept-Encoding", "gzip, deflate");

	c.setRequestProperty("Accept-Charset","utf-8");
	c.setRequestProperty("Accept-Encoding","utf-8");
        c.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");

	dos = c.openDataOutputStream();
	parameterString += "&data=" + data;
	byte[] requestBody = parameterString.getBytes();

	for (int i = 0;i < requestBody.length;i++)
	{
	    dos.writeByte(requestBody[i]);
	}

//	dos.flush();
	dos.close();

	rc = c.getResponseCode();

	if (rc != HttpConnection.HTTP_OK)
	{
	    c.close();
	    throw new IOException("HTTP response code: " + rc);
	}

	is = c.openInputStream();

	StringBuffer str = new StringBuffer();
	int ch;

	while ((ch = is.read()) != -1)
	{
	    str.append((char)ch);
	}

	c.close();

	String ret = new String(str);

//	System.out.println(ret);//debug

	return ret.getBytes();
    }

   public StatutConnexion testConnection(String url) throws IOException
    {
	HttpConnection c = null;
	StatutConnexion statutConnexion = null;
	int rc;

	c = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
	c.setRequestMethod(HttpConnection.POST);
	c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	//c.setRequestProperty("Accept-Encoding", "gzip, deflate");

	//ADD
	c.setRequestProperty("Accept-Charset","utf-8");
	c.setRequestProperty("Accept-Encoding","utf-8");
        c.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
	//End ADD

	try
	{
	    rc = c.getResponseCode();
	}
	catch (Exception e)
	{
	    statutConnexion = new StatutConnexion(StatutConnexion.WRONG_URL, "");

	    return statutConnexion;
	}

	statutConnexion = new StatutConnexion(c.getResponseCode(), c.getResponseMessage());

	return statutConnexion;
    }
}
